package com.div.popup;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.annotation.Nullable;

public class WindowManagerService extends Service implements View.OnClickListener {

    private static final int NOTIFICATION_ID = android.os.Process.myPid();
    private static final String NOTIFICATION_CHANNEL_ID = "PopupWindow";
    private static final String NOTIFICATION_CHANNEL_NAME = "SERVICE";
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mWindowManagerLp;
    private View mRootView;
    private CustomPopwindow mPopup;
    private View ivPopup;

    @Override
    public void onCreate() {
        super.onCreate();
        createWindowManager();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startForeground();
        showFloatWindow();
        return super.onStartCommand(intent, flags, startId);
    }

    private void showFloatWindow() {
        if (mWindowManager == null) {
            createWindowManager();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Settings.canDrawOverlays(WindowManagerService.this)) {
                LayoutInflater inflater = LayoutInflater.from(WindowManagerService.this);
                mRootView = inflater.inflate(R.layout.layout_window_manager, null);
                ivPopup = mRootView.findViewById(R.id.iv_popup);
                ivPopup.setOnClickListener(this);
                mRootView.findViewById(R.id.iv_close).setOnClickListener(this);
                mWindowManager.addView(mRootView, mWindowManagerLp);
                createPopupWindow();
            }
        }
    }

    private void createPopupWindow() {
        mPopup = new CustomPopwindow(WindowManagerService.this);
        mPopup.setView(R.layout.layout_popup_window)
                .setFocusable(true)
                .setOutsideTouchable(true)
                .setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
    }

    private void closeFloatWindow() {
        if (mWindowManager != null && mRootView != null) {
            mWindowManager.removeViewImmediate(mRootView);
            mWindowManager = null;
        }
    }

    private void startForeground() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
            nm.createNotificationChannel(channel);
            Notification.Builder builder = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID);
            builder.setContentTitle("PopupWindow");
            builder.setSmallIcon(R.mipmap.ic_launcher);
            Notification notification = builder.build();
            nm.notify(NOTIFICATION_ID, notification);
        }
    }

    private void createWindowManager() {
        mWindowManager = ((WindowManager) getSystemService(WINDOW_SERVICE));
        mWindowManagerLp = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mWindowManagerLp.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        }else {
            mWindowManagerLp.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        mWindowManagerLp.format = PixelFormat.TRANSLUCENT;
        mWindowManagerLp.gravity = Gravity.CENTER;
        mWindowManagerLp.flags =
                WindowManager.LayoutParams.FLAG_FULLSCREEN
                |WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                |WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                |WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        mWindowManagerLp.width = WindowManager.LayoutParams.MATCH_PARENT;
        mWindowManagerLp.height = dp2px(300);


    }

    private int dp2px(int dp) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics()) + 0.5F);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_close) {
            closeFloatWindow();
        }else if (v.getId() == R.id.iv_popup) {
            showPopupWindow();
        }
    }

    private void showPopupWindow() {
        mPopup.showAtLocation(ivPopup);
    }
}
